/**
 * 项目管理相关的API服务
 * 提供项目和项目成员的增删改查接口
 */

import { ApiResponse } from '../types';
import { request } from '../utils/request';

// 项目类型定义
export interface Project {
  id: number;
  name: string;
  description: string;
  status: number;
  ownerId: number;
  settings: string;
  createTime: string;
  updateTime: string;
  createBy: number;
  updateBy: number;
  deleted: number;
  remark: string;
}

// 项目成员类型定义
export interface ProjectMember {
  id: number;
  projectId: number;
  userId: number;
  role: number;
  permissions: string;
  joinedTime: string;
  createTime: string;
  updateTime: string;
  createBy: number;
  updateBy: number;
  deleted: number;
  remark: string;
}

/**
 * API基础URL
 */
const API_BASE_URL = '/api/projects';

/**
 * 创建项目
 * @param project 项目信息
 * @returns Promise<ApiResponse<Project>>
 */
export async function createProject(project: Omit<Project, 'id'>): Promise<ApiResponse<Project>> {
  return request<Project>(API_BASE_URL, {
    method: 'POST',
    body: JSON.stringify(project),
  });
}

/**
 * 更新项目
 * @param id 项目ID
 * @param project 项目信息
 * @returns Promise<ApiResponse<Project>>
 */
export async function updateProject(id: number, project: Partial<Project>): Promise<ApiResponse<Project>> {
  return request<Project>(`${API_BASE_URL}/${id}`, {
    method: 'PUT',
    body: JSON.stringify(project),
  });
}

/**
 * 删除项目
 * @param id 项目ID
 * @returns Promise<ApiResponse<void>>
 */
export async function deleteProject(id: number): Promise<ApiResponse<void>> {
  return request<void>(`${API_BASE_URL}/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 获取项目详情
 * @param id 项目ID
 * @returns Promise<ApiResponse<Project>>
 */
export async function getProject(id: number): Promise<ApiResponse<Project>> {
  return request<Project>(`${API_BASE_URL}/${id}`, {
    method: 'GET',
  });
}

/**
 * 获取项目列表
 * @returns Promise<ApiResponse<Project[]>>
 */
export async function listProjects(): Promise<ApiResponse<Project[]>> {
  return request<Project[]>(API_BASE_URL, {
    method: 'GET',
  });
}

/**
 * 添加项目成员
 * @param projectId 项目ID
 * @param member 项目成员信息
 * @returns Promise<ApiResponse<ProjectMember>>
 */
export async function addProjectMember(projectId: number, member: Omit<ProjectMember, 'id' | 'projectId'>): Promise<ApiResponse<ProjectMember>> {
  return request<ProjectMember>(`${API_BASE_URL}/${projectId}/members`, {
    method: 'POST',
    body: JSON.stringify(member),
  });
}

/**
 * 删除项目成员
 * @param projectId 项目ID
 * @param memberId 成员ID
 * @returns Promise<ApiResponse<void>>
 */
export async function removeProjectMember(projectId: number, memberId: number): Promise<ApiResponse<void>> {
  return request<void>(`${API_BASE_URL}/${projectId}/members/${memberId}`, {
    method: 'DELETE',
  });
}

/**
 * 获取项目成员列表
 * @param projectId 项目ID
 * @returns Promise<ApiResponse<ProjectMember[]>>
 */
export async function listProjectMembers(projectId: number): Promise<ApiResponse<ProjectMember[]>> {
  return request<ProjectMember[]>(`${API_BASE_URL}/${projectId}/members`, {
    method: 'GET',
  });
}